﻿using System;
using System.Data.Common;
using System.Data.SqlClient;
using System.Data.SqlServerCe;
using System.Collections.Generic;

namespace DataModel
{
    /// <summary>
    /// A repository for PatternLocation entities.
    /// </summary>
    public class PatternLocationRepository : RepositoryBase<PatternLocation>
    {
        #region Fields

        // Member variables
        private static Type m_ContextType = typeof(PatternDBEntities);
        private static string m_EdmName = "PatternDB";

        #endregion

        #region Constructor

        /// <summary>
        /// Default constructor
        /// </summary>
        /// <param name="filePath">A path to the target data file.</param>
        /// <remarks>
        /// Note that the constructor hard-codes the name of the Entity Data Model served
        /// by this Repository. We hard code the value, rather than pass it in, to isolate
        /// the caller from any knowledge of Entity Framework 4. The result is looser
        /// coupling between Entity Framework 4 and the rest of the application.
        /// </remarks>
        public PatternLocationRepository(string filePath)
            : base(filePath, m_ContextType, m_EdmName)
        {
        }

        /// <summary>
        /// Default constructor
        /// </summary>
        public PatternLocationRepository()
            : base(m_ContextType, m_EdmName)
        {
        }

        #endregion

        #region Public Methods

        public void AssignInstanceToLocation(int instanceID, int locationID)
        {
            var sqlCmd = string.Format(@"insert into InstanceLocation(PatternInstances_ID, PatternLocations_ID) values ({0}, {1})", instanceID, locationID);
            m_ObjectContext.ExecuteStoreCommand(sqlCmd);
            m_ObjectContext.SaveChanges();
        }

        public void RemoveInstanceFromLocation(int instanceID, int locationID)
        {
            var sqlCmd = string.Format(@"delete from InstanceLocation where PatternInstances_ID = {0} and PatternLocations_ID = {1}", instanceID, locationID);
            m_ObjectContext.ExecuteStoreCommand(sqlCmd);
            m_ObjectContext.SaveChanges();
        }

        #endregion
    }
}
